//
//  ChineseNotificationHelp.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/29.
//

#import "ChineseNotificationHelp.h"
@interface ChineseNotificationHelp ()

@property (nonatomic,strong)UITextField *textFiled;

@property (nonatomic,strong)NSString *beforeString;

/** 最大字数 */
@property (nonatomic,assign)NSInteger maxLength;

@end

@implementation ChineseNotificationHelp
+ (instancetype)ChineseNotificationHelpShareWithTextFiled:(UITextField *)textFiled WithMaxLength:(NSInteger)maxLength{
    static ChineseNotificationHelp *share = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[ChineseNotificationHelp alloc] init];
    });
    share.textFiled = textFiled;
    share.maxLength = maxLength;
    return share;
    
}
+ (instancetype)ChineseNotificationHelpShareWithTextFiled:(UITextField *)textFiled{

    static ChineseNotificationHelp *share = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        share = [[ChineseNotificationHelp alloc] init];
    });
    share.textFiled = textFiled;
    return share;

}
- (instancetype)init{

    self = [super init];

    if (self) {

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledEditChanged)

                                                   name:@"UITextFieldTextDidChangeNotification"

                                                 object:nil];

    }

    return self;

}
- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:nil];

}
-(void)textFiledEditChanged{

    [self textFiledTextChange:self.textFiled];

}
- (void)textFiledTextChange:(UITextField *)textField{

    NSString *string = textField.text;

    NSLog(@"1.=%@",textField.text);

    NSArray *currentar = [UITextInputMode activeInputModes];// 键盘输入模式

    UITextInputMode *current = [currentar firstObject];

    NSString *lang = current.primaryLanguage;

    if(![lang isEqualToString:@"zh-Hans"]) return;

    UITextRange *selectedRange = [textField markedTextRange];

    //获取高亮部分

    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];

    if(position) return;// 有高亮则不处理

    // 过滤非汉字

    NSMutableString *chinese = string.mutableCopy;

    for (int i = 0; i<string.length; i++) {

        int a = [string characterAtIndex:i];

        if (a > 0x4e00 && a < 0x9fff) continue;

        [chinese replaceCharactersInRange:NSMakeRange(i,1) withString:@" "];

         }

         // 除去所有空格

         textField.text = [self removeSpaceAndNewline:chinese];

    if (self.maxLength != 0) {
        NSInteger strLength = textField.text.length;
        if (strLength > self.maxLength) {
            textField.text = [textField.text substringToIndex:self.maxLength];
        }
    }
  
    
         NSLog(@"2.=%@",textField.text);

         if(self.beforeString && self.beforeString.length > textField.text.length && [chinese containsString:@" "]){

             //英文切换过程中处理出现删减的bug

             textField.text = self.beforeString;

             return;

         }

         self.beforeString = textField.text;

     }

     // 删除kon
- (NSString *)removeSpaceAndNewline:(NSString *)str

{

    NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];

    temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];

    temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    return temp;

}
@end
